import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // 设置请求和响应编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        // 简单验证（实际项目需连接数据库）
        if ("admin".equals(username) && "password123".equals(password)) {
            String token = JwtUtil.generateToken(username);

            // 创建登录成功页面
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>登录成功</title>");
            out.println("<style>");
            out.println("body { font-family: Arial, sans-serif; background-color: #f5f5f5; }");
            out.println(".container { max-width: 800px; margin: 50px auto; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }");
            out.println("h1 { color: #4CAF50; text-align: center; }");
            out.println(".token-box { background-color: #f0f8ff; padding: 15px; border-radius: 4px; border: 1px solid #b3d7ff; margin: 20px 0; font-family: monospace; word-wrap: break-word; }");
            out.println(".btn { display: inline-block; padding: 10px 20px; background: #4CAF50; color: white; text-decoration: none; border-radius: 4px; margin-top: 10px; }");
            out.println("</style>");
            out.println("</head>");
            out.println("<body>");
            out.println("<div class='container'>");
            out.println("<h1>登录成功!</h1>");
            out.println("<p>欢迎回来, " + username + "!</p>");
            out.println("<p>您的 JWT Token:</p>");
            out.println("<div class='token-box'>" + token + "</div>");
            out.println("<p>请复制此 Token 用于后续查询操作</p>");
            out.println("<a href='query.html' class='btn'>前往查询页面</a>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        } else {
            // 创建登录失败页面
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>登录失败</title>");
            out.println("<style>");
            out.println("body { font-family: Arial, sans-serif; background-color: #f5f5f5; }");
            out.println(".container { max-width: 800px; margin: 50px auto; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }");
            out.println("h1 { color: #f44336; text-align: center; }");
            out.println(".btn { display: inline-block; padding: 10px 20px; background: #2196F3; color: white; text-decoration: none; border-radius: 4px; margin-top: 20px; }");
            out.println("</style>");
            out.println("</head>");
            out.println("<body>");
            out.println("<div class='container'>");
            out.println("<h1>登录失败</h1>");
            out.println("<p>用户名或密码错误，请重试</p>");
            out.println("<a href='index.html' class='btn'>返回登录页面</a>");
            out.println("</div>");
            out.println("</body>");
            out.println("</html>");
        }
    }
}
